﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using System.Threading;
using DataRecv.enums;
using System.Runtime.InteropServices;
using DataRecv.tools;
using DataRecv.models.sdk.HKSDK;


namespace DataRecv.models.device
{
    public class HK9151 : Device
    {
        public new static Enum_CaptureSystem Ability =
            Enum_CaptureSystem.卡口 |
            Enum_CaptureSystem.逆行 |
            Enum_CaptureSystem.超速 |
            Enum_CaptureSystem.闯红灯 |
            Enum_CaptureSystem.压线 |
            Enum_CaptureSystem.不按导向行驶 |
            Enum_CaptureSystem.违章变道 |
            Enum_CaptureSystem.低速 |
            Enum_CaptureSystem.路口滞留 |
            Enum_CaptureSystem.机占非 |
            Enum_CaptureSystem.占专用车道 |
            Enum_CaptureSystem.禁左 |
            Enum_CaptureSystem.黄牌禁限 |
            Enum_CaptureSystem.违章停车;

        private CHCNetSDK.NET_DVR_DEVICEINFO_V30 m_struDeviceInfo;
        private CHCNetSDK.NET_DVR_SETUPALARM_PARAM m_SetAlarmParam;

        /// <summary>
        /// HK9151系列
        /// </summary>
        public HK9151(DeviceView view)
        {
            deviceview = view;
            state = new DeviceState();
            state.Port = 8000;
            state.Ping = false;
            state.LoginID = -1;
            state.Channel = -2;
            state.IsOpen = false;
            state.PlayHandle = -2;
            state.AlarmHandle = -1;
            m_struDeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();
            m_SetAlarmParam = new CHCNetSDK.NET_DVR_SETUPALARM_PARAM();
            m_SetAlarmParam.dwSize = (uint)Marshal.SizeOf(m_SetAlarmParam);//20
            try
            {
                m_SetAlarmParam.byLevel = (deviceview.deviceparam.bz.Contains("二级布防") ? (byte)1 : (byte)0);//布防优先级：0- 一等级（高），1- 二等级（中），2- 三等级（低，保留）
            }
            catch { m_SetAlarmParam.byLevel = 0; }
            //m_SetAlarmParam.byLevel = (deviceview.deviceparam.bz.Contains("二级布防") ? (byte)1 : (byte)0);//布防优先级：0- 一等级（高），1- 二等级（中），2- 三等级（低，保留）
            m_SetAlarmParam.byAlarmInfoType = 1;//上传报警信息类型（智能交通摄像机支持）：0- 老报警信息（NET_DVR_PLATE_RESULT），1- 新报警信息(NET_ITS_PLATE_RESULT)
        }

        public override void Init(object obj)
        {
            try
            {
                state.Ping = G.PingIP(deviceview.deviceparam.ip);
                state.PingOccurTime = DateTime.Now;
                if (PingReport != null) PingReport.Invoke(deviceview.deviceparam.ip, state.Ping);

                if (state.Ping == false)
                {
                    if (state.AlarmHandle >= 0)//如果是网络之前是连接的，需要撤防
                    {
                        CHCNetSDK.NET_DVR_CloseAlarmChan_V30(state.AlarmHandle);
                        state.AlarmHandle = -1;
                    }
                    if (state.LoginID >= 0)//如果是网络之前是连接的，需要注销
                    {
                        CHCNetSDK.NET_DVR_Logout_V30(state.LoginID);
                        state.LoginID = -1;
                    }
                    LogTool.log.Info("HK9151摄像机---" + deviceview.deviceparam.ip + "---Ping不通");
                    return;
                }

                //注册设备
                if (state.LoginID < 0)
                {
                    state.LoginID = CHCNetSDK.NET_DVR_Login_V30(deviceview.deviceparam.ip, state.Port, deviceview.deviceparam.deviceusername, deviceview.deviceparam.devicepassword, ref m_struDeviceInfo);
                    if (state.LoginID < 0) 
                    {
                        LogTool.log.Info("HK9151摄像机---" + deviceview.deviceparam.ip + "---注册失败");
                        return;
                    }
                }

                //启用布防
                if (state.AlarmHandle < 0)
                {
                    //m_AlarmHandle = CHCNetSDK.NET_DVR_SetupAlarmChan_V30(m_UserID);
                    state.AlarmHandle = CHCNetSDK.NET_DVR_SetupAlarmChan_V41(state.LoginID, ref m_SetAlarmParam);
                    if (state.AlarmHandle < 0)
                    {
                        LogTool.log.Info("HK9151摄像机---" + deviceview.deviceparam.ip + "---布防失败,code=" + CHCNetSDK.NET_DVR_GetLastError().ToString());

                        if (m_SetAlarmParam.byLevel == 0)
                        {
                            if (CHCNetSDK.NET_DVR_RebootDVR(state.LoginID) == true)
                            {
                                LogTool.log.Info("HK9151摄像机---" + deviceview.deviceparam.ip + "---重启");
                            }
                        }
                    }
                    else
                    {
                        LogTool.log.Info("HK9151摄像机---" + deviceview.deviceparam.ip + "---注册布防成功");
                    }
                }
            }
            catch (Exception er)
            {
                LogTool.log.Info("HK9151.Init()---" + deviceview.deviceparam.ip + "---" + er.Message);
            }
        }

        public override void ShutDown()
        {
            try
            {
                if (m_InitTimer != null)
                {
                    m_InitTimer.Dispose();
                }
                if (m_TimeAdjuster != null)
                {
                    m_TimeAdjuster.Dispose();
                }
                if (state.AlarmHandle >= 0)//撤销布防上传通道
                {
                    CHCNetSDK.NET_DVR_CloseAlarmChan_V30(state.AlarmHandle);
                    state.AlarmHandle = -1;
                }
                if (state.LoginID >= 0)//注销用户
                {
                    CHCNetSDK.NET_DVR_Logout_V30(state.LoginID);
                    state.LoginID = -1;
                }
            }
            catch { }
        }

        public override void AdjustTime(object obj)
        {
            //校时
            DateTime now = DateTime.Now;
            CHCNetSDK.NET_DVR_TIME CurTime = new CHCNetSDK.NET_DVR_TIME();
            CurTime.dwYear = (uint)now.Year;
            CurTime.dwMonth = (uint)now.Month;
            CurTime.dwDay = (uint)now.Day;
            CurTime.dwHour = (uint)now.Hour;
            CurTime.dwMinute = (uint)now.Minute;
            CurTime.dwSecond = (uint)now.Second;


            IntPtr ptime = Marshal.AllocHGlobal(Marshal.SizeOf(CurTime));
            try
            {
                Marshal.StructureToPtr(CurTime, ptime, true);
                bool ans = CHCNetSDK.NET_DVR_SetDVRConfig(state.LoginID, CHCNetSDK.NET_DVR_SET_TIMECFG, 0, ptime, (uint)Marshal.SizeOf(CurTime));
                LogTool.log.Info("HK9151摄像机---" + deviceview.deviceparam.ip + "---" + (ans ? "校时成功" : "校时失败"));
            }
            catch { }
            finally
            {
                Marshal.FreeHGlobal(ptime);
            }
        }
    }
}
